<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="10">
        <el-card shadow="hover">
          <Bar v-if="info.total.length > 0 && info.legend.length > 0" :legend="info.legend" :total="info.total" />
        </el-card>
        <el-card shadow="hover" style="margin-top: 20px">
          <Pie v-if="info.series.length > 0 && info.legend.length > 0" :legend="info.legend" :series="info.series" />
        </el-card>
      </el-col>

      <el-col :span="14">
        <el-card shadow="hover">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="物资名称" prop="name">
              <el-select v-model="queryParams.name" placeholder="物资名称" size="small" style="width: 100px;">
                <el-option
                  v-for="item in optionsName"
                  :key="item.name"
                  :label="item.name"
                  :value="item.name">
                </el-option>
              </el-select>
            </el-form-item>
              <el-form-item label="物资分类" prop="typeId">
                <el-select v-model="queryParams.typeId" placeholder="物资分类" size="small" style="width: 100px;">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.type"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" label-width="80px">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" label-width="80px">重置</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="loading" :data="goodinfoList">
            <el-table-column label="物资编号" align="center" prop="id" />
            <el-table-column label="物资名称" align="center" prop="name" />
            <el-table-column label="物资单位" align="center" prop="size" />
            <el-table-column label="库存" align="center" prop="total" />
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>

      </el-col>
    </el-row>

  </div>
</template>

<script>
import { listGoodinfo, getGoodinfo, delGoodinfo, addGoodinfo, updateGoodinfo } from "@/api/good/goodinfo";
import { listGoodtype } from "@/api/good/goodtype";
import Pie from "./Pie";
import Bar from "./Bar";

export default {

  name: "index",
  components: {Pie, Bar},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物资信息表格数据
      goodinfoList: [],
      // 物资类型表格数据
      goodtypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      optionsName: [],
      options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        name: null,
        size: null,
        total: null,
      },
      totalList: [],
      typeList: [],
      info: {
        legend: [],
        total: [],
        series: []
      },

      // 表单参数
      form: {},
      typeList:[],
    };
  },
  created() {
    this.getList();
    this.getTypeList();
    this.getInfoList();
  },
  methods: {
    /**查询物资类型信息*/
    getTypeList(){
      //console.log("进")
      this.loading = true;
      listGoodtype().then(response => {
       //console.log(response)
        this.options = response.rows;
      })
    },
    getInfoList(){
      listGoodinfo().then(response => {
        //console.log(response)
        this.optionsName = response.rows;
      })
    },
    /** 查询物资信息列表 */
    getList() {
      this.loading = true;
      listGoodinfo(this.queryParams).then(response => {
        this.goodinfoList = response.rows;
        //console.log(this.goodinfoList);
        this.total = this.goodinfoList.total;
        this.totalList = this.goodinfoList;
        this.loading = false;
        if (this.queryParams){
          this.info.legend =this.totalList.map(item => {
            return item.name
          })
          this.info.total = this.totalList.map(item => {
            return item.total
          })
          this.info.series = this.totalList.map(item => {
            return {
              value: item.total,
              name: item.name
            }
          })
        }
        this.total = response.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

  }
};
</script>
